package com.example.app.personManagement;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.app.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
        implements View.OnClickListener {
    private Context mContext;
    private List<Person> personList;
    private OnRecyclerViewItemClickListener mOnItemClickListener ;

    private int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
    public ListAdapter() {
        personList = new ArrayList<>();
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v,(Person)v.getTag());
        }
    }
    @Override
    public void onViewRecycled(ViewHolder holder){
        holder.itemView.setOnLongClickListener(null);
        super.onViewRecycled(holder);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Person note);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_person,parent,false);

        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Person note = personList.get(position);
        holder.itemView.setTag(note);
        holder.tv_list_title.setText(note.getName());
        StringBuffer buffer = new StringBuffer();
        buffer.append("编号:").append(note.getNumber()).append("    ");
        buffer.append("性别:").append(note.getSex()).append("    ");
        buffer.append("年龄:").append(note.getAge()).append("\n");
        buffer.append("所属部门:").append(note.getDepartment()).append("    ");
        buffer.append("职务:").append(note.getJob()).append("\n");
        buffer.append("薪资:").append(note.getSalary()).append("\n");
        buffer.append("电话:").append(note.getPhone()).append("\n");

        holder.tv_list_summary.setText(buffer.toString());

    }

    @Override
    public int getItemCount() {
        if (personList != null && personList.size()>0){
            return personList.size();
        }
        return 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder   {
        public TextView tv_list_title;
        public TextView tv_list_summary;

        public ViewHolder(View view){
            super(view);
            tv_list_title = (TextView) view.findViewById(R.id.item_main_note_title_view);
            tv_list_summary = (TextView) view.findViewById(R.id.item_main_note_content_view);
        }


    }
}
